library(tidyverse)
library(gridExtra)
library(extrafont)
library(vdemdata)

#### Liberal Democracy Index ####
dat2 <- vdem %>%
  filter (country_name == "Philippines", year >= 2000) %>%
  select(country_name, year, v2x_polyarchy, v2x_libdem)

p1 <- ggplot(dat2, aes(x = year, y = v2x_libdem)) +
  geom_line(size = 0.5, color = "black") + xlim(2002.5, 2022) + ylim(0.2, 0.5) +
  labs(x = "Year", y = "Liberal Democracy Index") + 
  theme_classic() + theme(text = element_text(family = "Times New Roman")) +
  geom_vline(xintercept = 2010, size = 0.2) +
  geom_vline(xintercept = 2016, size = 0.2) +
  ggtitle("(a) Liberal Democracy Index") +
  theme(plot.title = element_text(size = rel(1), lineheight = .9)) 

g1 <- p1 + theme(text = element_text(family = "Times New Roman")) 

#### approval rating ####
dat <- read.csv("presap_martial.csv")

p2 <- ggplot(dat%>%filter(type == "president"), aes(x = number, y = approve)) + geom_line(size = 0.45) +
  xlim(1, 71) + ylim(0, 110) +
  theme_classic() + theme(text = element_text(family = "Times New Roman")) +
  scale_colour_brewer(palette = "Set1") +
  geom_vline(xintercept = 50, size = 0.2) +
  geom_vline(xintercept = 28, size = 0.2) + 
  annotate("text", x = 11, y = 104, label = "Arroyo\n (2001.1 - 2010.6)", size = 3,color = "black") +
  annotate("text", x = 39, y = 104, label = "Aquino III\n (2010.7 - 2016.6)", size = 3,color = "black") +
  annotate("text", x = 62, y = 104, label = "Duterte\n (2016.7 - 2022.6)", size = 3,color = "black") +
  xlab("Presidential term") + ylab("Approval for president (%)") +
  ggtitle("(b) Presidents' approval rating") +
  theme(plot.title=element_text(size = rel(1), lineheight = .9)) +
  guides(fill = guide_legend(title = NULL))

p3 <- p2 + theme(axis.text.x = element_blank())

#### Figure 1 ####
p4 <- grid.arrange(g1, p3, nrow = 2)
ggsave(filename = "Figure_1.pdf", plot = p4, width = 13, device = cairo_pdf,
       height = 15, units = "cm")